package com.liyunc.demo.model.repository;

import com.liyunc.demo.model.entity.Resource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ResourceRepository extends JpaRepository<Resource, Long> {
    @Query(value = "SELECT *"
        + " FROM resource"
        + " WHERE id"
        + " IN (SELECT resources_id"
        + " FROM role_resources"
        + " WHERE role_id"
        + " IN (SELECT user_roles.roles_id"
        + " FROM user_roles"
        + " LEFT JOIN user ON user.id=user_roles.user_id"
        + " WHERE user.username = ?1))", nativeQuery = true)
    List<Resource> getResourcesByUserName(String username);

}
